﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace BTSZap
{
    internal static class ReflectionCall
    {
        public static void VoidCall(this object instance,string methodName, params object[] args)
        {
            instance.GetType().InvokeMember(methodName, BindingFlags.InvokeMethod, null,instance, args);
        }
        public static object ObjectCall(this object instance, string methodName, params object[] args)
        {
            return instance.GetType().InvokeMember(methodName, BindingFlags.InvokeMethod, null, instance, args);
        }
        public static T Call<T>(this object instance, string methodName, params object[] args) 
        {
            return (T) instance.GetType().InvokeMember(methodName, BindingFlags.InvokeMethod, null, instance, args);
        }

        public static Object CreateInstance(this Type typ, params object[] args)
        {
            return Activator.CreateInstance(typ, args);
        }

        public static T CreateInstance<T>(this Type typ, params object[] args)
        {
            return (T) Activator.CreateInstance(typ, args);
        }

        public static void SetProperty(this object instance, string property, object value)
        {
            instance.GetType().InvokeMember(property, BindingFlags.SetProperty, null,instance, new object[]{value});
        }

        public static object GetProperty(this object instance, string property)
        {
            return instance.GetType().InvokeMember(property, BindingFlags.GetProperty,null, instance,null);
        }

        public static T GetProperty<T>(this object instance, string property)
        {
            return (T)instance.GetType().InvokeMember(property, BindingFlags.GetProperty, null, instance,null);
        }
    }
}